var _oXml = CreateXmlDocument();

function GetGuid()
{
var sGuid = null;

try
{
var oCommand = new RemoteCommand("SystemCustomization", "GetGuid");
var oResult = oCommand.Execute();
if (oResult.Success)
{
sGuid = oResult.ReturnValue;
}
}
catch (e)
{
sGuid = null;
}

if (!sGuid)
{

openErrorDlg("0x80043b10");
}

return sGuid;
}

function SetActiveObject()
{
var oObj = GetObject(event.srcElement);

if (oObj != null)
{

if ((_oActive) && (_oActive.className.indexOf("ms-crm-Tab") == -1))
{

_oActive.style.border = "1px dashed #cccccc";
}

_oActive = oObj;

if (_oActive.className.indexOf("ms-crm-Tab") == -1)
{
_oActive.style.border = "1px solid #23b30c";
}

return true;
}

return false;
}


function GetObject(oObj)
{

if(oObj.className == "imgLock")
{
oObj = oObj.parentElement;

}

if (oObj.tagName == "TD")
{
switch (oObj.className)
{
case "field":
case "field reqField":
case "field recField":
case "rofield":
case "label":
case "label ms-crm-Field-Required":
case "label ms-crm-Field-Recommended":
case "field reqField":
case "field recField":
oObj = oObj.parentElement.parentElement.parentElement.parentElement;
break;

case "section":
case "":
oObj = oObj.parentElement.parentElement.parentElement;
break;
}
}

if (_bPreviewMode && (oObj.className == "section" || oObj.className.indexOf("ms-crm-Tab") != -1))
{
oObj = null;
}

return oObj;
}


function GetDescByLangCode(oObjs)
{
var i;
var iLen = oObjs.length;

for (i = 0; i < iLen; i++)
{
if (oObjs[i].LanguageCode == USER_LANGUAGE_CODE)
{
return oObjs[i].Description;
}
}

return "";
}


function GetLocalizedObjsArray(oNodes)
{
var oObjs = new Array();

var oNode;

var i;
var iLen = oNodes.length;

for (i = 0; i < iLen; i++)
{
oNode = oNodes[i];

oObjs[i] =	new LocalizedObj(oNode.getAttribute("description"), oNode.getAttribute("languagecode"));
}

return oObjs;
}

function GetEventObjsArray(oNodes)
{
var oObjs = new Array();

var oNode;
var i, j;
var iLen = oNodes.length;
var oDependsNodes;
var oDNode;


for (i = 0; i < iLen; i++)
{
oNode = oNodes[i];



oDependsNodes = oNode.selectNodes("dependencies/dependency");
iDependsLen = oDependsNodes.length;

var oDependsAry = new Array(iDependsLen);

for (j = 0; j < iDependsLen; j++)
{
oDependsAry[j] = oDependsNodes[j].getAttribute("id");
}

oObjs[i] =	new EventObj(oNode.getAttribute("name"), (oNode.getAttribute("active") == "true"), (oNode.getAttribute("application") == "true"), oNode.selectSingleNode("script").text, oDependsAry);
}

if (oObjs.length == 0)
{
oObjs = null;
}

return oObjs;
}

function GetParameterBag(oNodes)
{
if(oNodes == null || oNodes.length == 0) return null;
var oObjs = new Object();
var oNode, i, iLen = oNodes.length;

for (i = 0; i < iLen; i++)
{
oNode = oNodes[i];
oObjs[oNode.tagName] = oNode.text;
}

return oObjs;
}

function PersistLocalizedNodes(oXml, sExpr, oObjs)
{
var oObj;

var oParentNode;
var oNode;

var i;
var iLen = oObjs.length;

for (i = 0; i < iLen; i++)
{
oObj = oObjs[i];

oNode = oXml.selectSingleNode(sExpr + "[@languagecode = '" + oObj.LanguageCode + "']");

if (oNode)
{
oNode.setAttribute("description", oObj.Description);
}
else
{
oParentNode = oXml.selectSingleNode(sExpr.substring(0, sExpr.lastIndexOf("/")));

oNode = oXml.createElement(sExpr.substr(sExpr.lastIndexOf("/") + 1));

oNode.setAttribute("description", oObj.Description);
oNode.setAttribute("languagecode", oObj.LanguageCode);

oParentNode.appendChild(oNode);
}
}
}

function GetCurrentTabObject()
{
var i;
var iLen = tabs.children.length;

for (i = 0; i < iLen; i++)
{
if ("ms-crm-Tab ms-crm-Tab-Selected" == tabs.children[i].className)
{
return tabs.children[i];
}
}
}

function GetCurrentTabName(oActive)
{
if (oActive.parentElement.className == "ms-crm-Tab ms-crm-Tab-Selected")
{
oActive = oActive.parentElement;
}
switch (oActive.className)
{
case "ms-crm-Tab ms-crm-Tab-Selected":

return oActive.name;

break;

case "section":

return GetTabName(oActive.parentElement.parentElement.parentElement.parentElement.parentElement.id);

break;

case "cell":
case "iframe":

return GetTabName(oActive.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.id);

break;
}
}


function GetCurrentSectionName(oActive)
{
if (oActive.parentElement.className == "ms-crm-Tab ms-crm-Tab-Selected")
{
oActive = oActive.parentElement;
}
switch (oActive.className)
{
case "ms-crm-Tab ms-crm-Tab-Selected":

return "";

break;

case "section":

return oActive.name;

break;

case "cell":
case "iframe":

return oActive.parentElement.parentElement.parentElement.name;

break;
}
}


function Gettabid(sTabName)
{
var i
var iLen = tabs.children.length;

for (i = 0; i < iLen; i++)
{
if (sTabName == tabs.children[i].name)
{
return tabs.children[i].tabid;
}
}
}

function GetTabName(stabid)
{
var i
var iLen = tabs.children.length;

for (i = 0; i < iLen; i++)
{
if (tabs.children[i].tabid == stabid)
{
return tabs.children[i].name;
}
}
}


function ClickTabByName(sTabName)
{
var i
var iLen = tabs.children.length;

for (i = 0; i < iLen; i++)
{
if (sTabName == tabs.children[i].name)
{
tabs.children[i].click();

break;
}
}
}


function ViewPropertiesNow()
{
if (SetActiveObject())
{
ViewProperties(_oActive);
}
}


function ViewProperties(oActive)
{
if (oActive != null)
{
if (oActive.parentElement.className == "ms-crm-Tab ms-crm-Tab-Selected")
{
oActive = oActive.parentElement;
}
switch (oActive.className)
{
case "ms-crm-Tab ms-crm-Tab-Selected":

UpdateTab(oActive);

break;

case "section":

UpdateSection(oActive);

break;

case "cell":

UpdateField(oActive);

break;

case "iframe":

UpdateIFrame(oActive);

break;
}
}
else
{
alert(LOCID_FORMED_MUSTSELECT);
}
}


function Remove(oActive)
{
if (oActive != null)
{
if (oActive.parentElement.className == "ms-crm-Tab ms-crm-Tab-Selected")
{
oActive = oActive.parentElement;
}
switch (oActive.className)
{
case "ms-crm-Tab ms-crm-Tab-Selected":

if (confirm(LOCID_FORMED_CONFIRMREMOVETAB))
{
return RemoveTab(oActive);
}

break;

case "section":

if (confirm(LOCID_FORMED_CONFIRMREMOVESEC))
{
return RemoveSection(oActive);
}

break;

case "cell":
case "iframe":

if (confirm(LOCID_FORMED_CONFIRMREMOVEFLD))
{
return RemoveField(oActive, true);
}

break;
}
}
else
{
alert(LOCID_FORMED_MUSTSELECT);
}

return false;
}




function IsRemovable(oNodes)
{
var sFieldName;

var i;
var iLen = oNodes.length;

for (i = 0; i < iLen; i++)
{

sFieldName = oNodes[i].getAttribute("id");


if (sFieldName)
{
var oCell = GetCellNode(sFieldName);

if(oCell.getAttribute("locklevel") == "1")
{
return false;
}

var oFieldNode = oFieldsXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']");

if(!_bPreviewMode && oFieldNode)
{
if (oFieldNode.getAttribute("requiredforplatform") == "true" &&
(oFieldNode.getAttribute("validforcreateapi") == "true"	||
oFieldNode.getAttribute("validforupdateapi") == "true"))
{
return false;
}

if (oFieldNode.getAttribute("requiredforform") == "true")
{
return false;
}

if (oPropertiesXml.selectSingleNode("/entity/fields/field[@name = '" + sFieldName + "']/@requiredlevel").text == "required")
{
return false;
}
}

}
}

return true;
}

function IsDependent(oNodes)
{
var sFieldName;

var i;
var iLen = oNodes.length;

for (i = 0; i < iLen; i++)
{

sFieldName = oNodes[i].getAttribute("id");

if (sFieldName)
{
if (oFormXml.selectNodes("/entity/form/tabs/tab/sections/section/rows/row/cell/events/event/dependencies/dependency[@id = '" + sFieldName + "']").length > 0)
{
return true;
}

if (oFormXml.selectNodes("/entity/form/events/event/dependencies/dependency[@id = '" + sFieldName + "']").length > 0)
{
return true;
}

if (oFormXml.selectNodes("/entity/form/externaldependencies/dependency[@id = '" + sFieldName + "']").length > 0)
{
return true;
}
}
}

return false;

}


function GetDependentList(oNodes,bIncludeFieldName)
{
var sFieldName;
var sfieldList = "";
var oDependents = new Array();
var i;
var j;
var iIndex = 0;
var iLen = oNodes.length;

for (i = 0; i < iLen; i++)
{

sFieldName = oNodes[i].getAttribute("id");

if (sFieldName)
{
var oDependentNodes = oFormXml.selectNodes("/entity/form/tabs/tab/sections/section/rows/row/cell/events/event/dependencies/dependency[@id = '" + sFieldName + "']");
var iDepLen = oDependentNodes.length;

for (j = 0; j < iDepLen; j++)
{
var oControl = oDependentNodes[j].parentNode.parentNode.parentNode.parentNode.selectSingleNode("control");
var bSystem = oDependentNodes[j].parentNode.parentNode.getAttribute("application") == "true";
var dependencyName = oControl.getAttribute("id");







if(dependencyName.substr(0,7) == "IFRAME_")
{
oDependents[iIndex++] = new DependencyObj(sFieldName,"iframe",dependencyName, bSystem);
}
else
{
oDependents[iIndex++] = new DependencyObj(sFieldName,"field",dependencyName, bSystem);
}

}

oDependentNodes = oFormXml.selectNodes("/entity/form/events/event/dependencies/dependency[@id = '" + sFieldName + "']");
iDepLen = oDependentNodes.length;

for (j = 0; j < iDepLen; j++)
{
var bSystem = oDependentNodes[j].parentNode.parentNode.getAttribute("application") == "true";
oDependents[iIndex++] = new DependencyObj(sFieldName,"form",oDependentNodes[j].parentNode.parentNode.getAttribute("name"), bSystem);
}

oDependentNodes = oFormXml.selectNodes("/entity/form/externaldependencies/dependency[@id = '" + sFieldName + "']");
iDepLen = oDependentNodes.length;

for (j = 0; j < iDepLen; j++)
{
oDependents[iIndex++] = new DependencyObj(sFieldName,"external","",false);
}

}
}

iDepLen = oDependents.length;
for (i = 0; i < iDepLen; i++)
{
var sFieldLocName = bIncludeFieldName ? GetFieldDisplayName(oDependents[i].FieldName) + " - " : "";
var sDependencyType = oDependents[i].System ? LOCID_FORMED_SYSTEMDEP : LOCID_FORMED_CUSTOMDEP;
switch(oDependents[i].Type)
{
case "field":
sfieldList += formatString(LOCID_FORMED_FIELDDEP, sFieldLocName, GetEventLocName("onchange"), GetFieldDisplayName(oDependents[i].Name), sDependencyType) + "\n";
break;

case "iframe":
sfieldList += formatString(LOCID_FORMED_IFRAMEDEP, sFieldLocName, GetFieldDisplayName(oDependents[i].Name), sDependencyType) + "\n";
break;

case "form":
sfieldList += formatString(LOCID_FORMED_FORMDEP, sFieldLocName, GetEventLocName(oDependents[i].Name), sDependencyType) + "\n";
break;

case "external":
sfieldList += formatString(LOCID_FORMED_EXTDEP, sFieldLocName) + "\n";
break;
}
}
return sfieldList;
}

function GetEmptyCellNode()
{
var oCell = oFormXml.createElement("cell");

oCell.appendChild(oFormXml.createElement("labels"));

return oCell;
}

function GetTabNode(sTabName)
{
return oFormXml.selectSingleNode("/entity/form/tabs/tab[@name = '" + sTabName + "']");
}

function GetSectionsNode(sTabName)
{
return oFormXml.selectSingleNode("/entity/form/tabs/tab[@name = '" + sTabName + "']/sections");
}


function GetSectionNode(sSectionName)
{
return oFormXml.selectSingleNode("/entity/form/tabs/tab/sections/section[@name = '" + sSectionName + "']");
}


function GetRowsNode(sSectionName)
{
return oFormXml.selectSingleNode("/entity/form/tabs/tab/sections/section[@name = '" + sSectionName + "']/rows");
}


function GetRowNode(sFieldName)
{
return oFormXml.selectSingleNode("/entity/form/tabs/tab/sections/section/rows/row[cell/control/@id = '" + sFieldName + "']");
}


function GetCellNode(sFieldName)
{
return oFormXml.selectSingleNode("/entity/form/tabs/tab/sections/section/rows/row/cell[control/@id = '" + sFieldName + "']");
}

function ViewFormProperties()
{

var oArgs = new Object();
oArgs.Event			= null;
oArgs.FormXml		= oFormXml;
oArgs.PropertiesXml	= oPropertiesXml;
oArgs.FieldsXml		= oFieldsXml;
var oEventNodes		= oFormXml.selectNodes("/entity/form/events/event[@application = 'false']");
var oEvents = GetEventObjsArray(oEventNodes);


var oFormNode = oFormXml.selectSingleNode("/entity/form");
var bEnableFormAssistant = (oFormNode.getAttribute("enablerelatedinformation") == "true");
var bFormAssistantCollapsed = (oFormNode.getAttribute("relatedInformationCollapsed") == "true");

var oDependsNodes = oFormXml.selectNodes("/entity/form/externaldependencies/dependency");
var iDependsLen = oDependsNodes.length;
var oDependsAry = new Array(iDependsLen);

for (j = 0; j < iDependsLen; j++)
{
oDependsAry[j] = oDependsNodes[j].getAttribute("id");
}

var oFormObj = new FormObj(oEvents, oDependsAry, bEnableFormAssistant, !bFormAssistantCollapsed);
oArgs.FormProperties = oFormObj;
var oUpdatedFormObj = openStdDlg(prependOrgName("/Tools/FormEditor/Dialogs/formProperties.aspx"), oArgs, 650, 550 );


if (oUpdatedFormObj)
{
if (oUpdatedFormObj.Events)
{
var iEventsLen = oUpdatedFormObj.Events.length;

for (var k = 0; k < iEventsLen; k++)
{


if (oUpdatedFormObj.Events[k].Dependencies != null)
{
var oEventNode = oFormXml.selectSingleNode("/entity/form/events/event[@application = 'false' and @name = '" + oUpdatedFormObj.Events[k].Name + "']");
PersistFormEventXml(oEventNode,oUpdatedFormObj.Events[k]);
}
}
}


oFormNode.setAttribute("enablerelatedinformation", oUpdatedFormObj.EnableFormAssistant.toString());
oFormNode.setAttribute("relatedInformationCollapsed", (!oUpdatedFormObj.FormAssistantExpanded).toString());

if (oUpdatedFormObj.Dependencies)
{
var iNewLen	= oUpdatedFormObj.Dependencies.length;

var oParentNode = oFormXml.selectSingleNode("/entity/form/externaldependencies");
if(!oParentNode)
{
oParentNode = oFormXml.createElement("externaldependencies");
var oNode = oFormXml.selectSingleNode("/entity/form");
oNode.appendChild(oParentNode);
}
var oChildNodes = oParentNode.selectNodes("dependency");
oChildNodes.removeAll;

if(iNewLen == 0)
{
oParentNode.parentNode.removeChild(oParentNode);
}
else
{
for (i = 0; i < iNewLen; i++)
{
oNewNode = oFormXml.createElement("dependency");
oNewNode.setAttribute("id", oUpdatedFormObj.Dependencies[i]);
oParentNode.appendChild(oNewNode);
}
}
}
}
}




function PersistFormEventXml(oEventNode,oUpdatedEventObj)
{
var oNewNode;
var oNode;



if (!oEventNode)
{
var oEvents = oFormXml.selectSingleNode("/entity/form/events");

if(!oEvents)
{
oEvents = oFormXml.createElement("events");
var oNode = oFormXml.selectSingleNode("/entity/form");
oNode.appendChild(oEvents);
}

oEventNode = oFormXml.createElement("event");
oEvents.appendChild(oEventNode);

var oScript = oFormXml.createElement("script");
oEventNode.appendChild(oScript);
oScript.appendChild(oFormXml.createCDATASection(" "));

var oDepends = oFormXml.createElement("dependencies");
oEventNode.appendChild(oDepends);

oEventNode.setAttribute("name", oUpdatedEventObj.Name);
oEventNode.setAttribute("application", "false");
}

oEventNode.setAttribute("active", ( (1 == oUpdatedEventObj.Active) ? "true" : "false") );

var iNewLen	= oUpdatedEventObj.Dependencies.length;

var oParentNode = oEventNode.selectSingleNode("dependencies");
var oChildNodes = oParentNode.selectNodes("dependency");
oChildNodes.removeAll;

for (i = 0; i < iNewLen; i++)
{
oNewNode = oFormXml.createElement("dependency");
oNewNode.setAttribute("id", oUpdatedEventObj.Dependencies[i]);
oParentNode.appendChild(oNewNode);
}

var oScriptNode = oEventNode.selectSingleNode("script");
oScriptNode.firstChild.replaceData(0, oScriptNode.firstChild.length, oUpdatedEventObj.Script);

}



function GetFieldDisplayName(sFieldName)
{
var sDisplayName = "";
var oFieldNode = oFieldsXml.selectSingleNode("/entity/fields/field[@name='" + sFieldName + "']");
if(oFieldNode)
{
var oPropertiesNode = oPropertiesXml.selectSingleNode("/entity/fields/field[@name='" + sFieldName + "']");
if(oPropertiesNode != null)
{
sDisplayName = oPropertiesNode.selectSingleNode("displaynames/displayname[@languagecode = " + USER_LANGUAGE_CODE + "]").getAttribute("description");
}
else
{
var sFieldLocName	= oFieldsXml.selectSingleNode("/entity/fields/field[@name='" + sFieldName + "']").getAttribute("localizedname");
sDisplayName = ((sFieldLocName) ? sFieldLocName : sFieldName);
}
}

else
{
sDisplayName =  oFormXml.selectSingleNode("/entity/form/tabs/tab/sections/section/rows/row/cell[control/@id = '" + sFieldName + "']/labels/label[@languagecode = " + USER_LANGUAGE_CODE + "]").getAttribute("description");
}

return sDisplayName;
}

function GetEventLocName(sEventName)
{
var sEventLocName = "";
switch(sEventName)
{
case "onload":
sEventLocName = LOCID_FORMED_ONLOAD;
break;

case "onsave":
sEventLocName = LOCID_FORMED_ONSAVE;
break;

case "onchange":
sEventLocName = LOCID_FORMED_ONCHANGE;
break;

default:
sEventLocName = LOCID_FORMED_EVENT;
break;
}

return sEventLocName;

}




var _ControlClassIds =
{
iframe:				"{FD2A7985-3187-444e-908D-6624B21F69C0}",
lookup:				"{270BD3DB-D9AF-4782-9025-509E298DEC0A}",
regarding:			"{F3015350-44A2-4aa0-97B5-00166532B5E9}",
partylist:			"{CBFB742C-14E7-4a17-96BB-1A13F7F64AA2}",
duration:			"{AA987274-CE4E-4271-A803-66164311A958}",
integer:			"{C6D124CA-7EDA-4a60-AEA9-7FB8D318B68F}",
float:				"{0D2C745A-E5A8-4c8f-BA63-C6D3BB604660}",
money:				"{533B9E00-756B-4312-95A0-DC888637AC78}",
decimal:			"{C3EFE0C3-0EC6-42be-8349-CBD9079DFD8E}",
email:				"{ADA2203E-B4CD-49be-9DDF-234642B43B52}",
text:				"{4273EDBD-AC1D-40d3-9FB2-095C621B552D}",
textarea:			"{E0DECE4B-6FC8-4a8f-A065-082708572369}",
url:				"{71716B6C-711E-476c-8AB8-5D11542BFB47}",
tickersymbol:		"{1E1FC551-F7A8-43af-AC34-A8DC35C7B6D4}",
memo:				"{E0DECE4B-6FC8-4a8f-A065-082708572369}",
picklist:			"{3EF39988-22BB-4f0b-BBBE-64B5A3748AEE}",
status:				"{5D68B988-0661-4db2-BC3E-17598AD3BE6C}",
datetime:			"{5B773807-9FB2-42db-97C3-7A91EFF8ADFF}",
boolean_radio:		"{67FAC785-CD58-4f9f-ABB3-4B7DDC6ED5ED}",
boolean_checkbox:	"{B0C6723A-8503-4fd7-BB28-C8A06AC933C2}",
language:			"{671A9387-CA5A-4d1e-8AB7-06E39DDCF6B5}",
timezone:			"{7C624A0B-F59E-493d-9583-638D34759266}"
}




var _oDataTypes =
{
unknown:	"unknown",
customer:	"customer",
owner:		"owner",
lookup:		"lookup",
partylist:	"partylist",
integer:	"integer",
float:		"float",
money:		"money",
decimal:	"decimal",
text:		"text",
memo:		"memo",
boolean:	"boolean",
picklist:	"picklist",
status:		"status",
state:		"state",
datetime:	"datetime",
iframe:		"iframe"
};

var _oDataTypeFormats =
{

regarding:		"regarding",


duration:		"duration",
timezone:		"timezone",
language:		"language",


email:			"email",
text:			"text",
textarea:		"textarea",
url:			"url",
tickersymbol:	"tickersymbol"
};




function GetControlClassId(sDataType, sFormat)
{
switch (sDataType)
{
case _oDataTypes.customer:
case _oDataTypes.owner:
return _ControlClassIds.lookup;

case _oDataTypes.lookup:
if (sFormat != null && sFormat == _oDataTypeFormats.regarding)
{
return _ControlClassIds.regarding;
}
else
{
return _ControlClassIds.lookup;
}

case _oDataTypes.partylist:
return _ControlClassIds.partylist;

case _oDataTypes.integer:
switch (sFormat)
{
case _oDataTypeFormats.duration:
return _ControlClassIds.duration;

case _oDataTypeFormats.timezone:
return _ControlClassIds.timezone;

case _oDataTypeFormats.language:
return _ControlClassIds.language;
}
return _ControlClassIds.integer;

case _oDataTypes.float:
return _ControlClassIds.float;

case _oDataTypes.money:
return _ControlClassIds.money;

case _oDataTypes.decimal:
return _ControlClassIds.decimal;

case _oDataTypes.text:
switch (sFormat)
{
case _oDataTypeFormats.email:
return _ControlClassIds.email;
case _oDataTypeFormats.text:
return _ControlClassIds.text;
case _oDataTypeFormats.textarea:
return _ControlClassIds.textarea;
case _oDataTypeFormats.url:
return _ControlClassIds.url;
case _oDataTypeFormats.tickersymbol:
return _ControlClassIds.tickersymbol;
}
break;

case _oDataTypes.memo:
return _ControlClassIds.memo;

case _oDataTypes.boolean:
return _ControlClassIds.boolean_radio;

case _oDataTypes.picklist:
case _oDataTypes.state:
return _ControlClassIds.picklist;

case _oDataTypes.status:
return _ControlClassIds.status;

case _oDataTypes.datetime:
return _ControlClassIds.datetime;

case _oDataTypes.iframe:
return _ControlClassIds.iframe;
}

return _ControlClassIds.text;
}

function CanSpanColumns(sControlClassId)
{
switch (sControlClassId)
{
case _ControlClassIds.iframe:
case _ControlClassIds.memo:
case _ControlClassIds.textarea:
return false;
}

return true;
}

function CanSpanRows(sControlClassId)
{
switch (sControlClassId)
{
case _ControlClassIds.iframe:
case _ControlClassIds.memo:
case _ControlClassIds.textarea:
return true;
}

return false;
}
